﻿using UnityEngine;
using System.Collections;

public class GameManage : MonoBehaviour {

	public GameObject player;
	private GameObject currentPlayer;
	private GameCam cam;

	void Start () {
		cam = GetComponent<GameCam>();
		SpawnPlayer(Vector3.zero);

	}
	

	private void SpawnPlayer(Vector3 spawnPos)
	{
		currentPlayer = Instantiate(player, spawnPos, Quaternion.identity)as GameObject;
		cam.SetTarget(currentPlayer.transform);
	}

	private void Update()
	{
		//checks if player is falling down, respawns him
		if(transform.position.y < -10)
		{
			Destroy(currentPlayer);

			if(InventoryController.lives > 1)
			{
				InventoryController.lives--;
				SpawnPlayer(Vector3.zero); //Instantiate "new" Player
			}
			else if(InventoryController.lives == 1)
			{
				InventoryController.lives--;
				//Game Over - to do!
			}
		}
	}
}
